package com.gilight.base.service.impl;

import com.gilight.base.base.BusinessException;
import com.gilight.base.dao.SysDataRuleMapper;
import com.gilight.base.dao.SysPermissionMapper;
import com.gilight.base.dto.DataRuleDto;
import com.gilight.base.model.SysDataRule;
import com.gilight.base.service.SysDataRuleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据规则业务接口实现
 */
@Service
public class SysDataRuleServiceImpl implements SysDataRuleService {

    @Autowired
    private SysDataRuleMapper ruleMapper;
    @Autowired
    private SysPermissionMapper sysPermissionMapper;
    /**
     * 列表
     * @param dataRuleDto 查询条件
     * @return
     */
    @Override
    public PageInfo findList(DataRuleDto dataRuleDto) {
        Map<String, Object> map = new HashMap();
        if (StringUtils.isNotEmpty(dataRuleDto.getRuleName())) {
            map.put("roleName", "%" + dataRuleDto.getRuleName() + "%");
        }
        PageHelper.startPage(dataRuleDto.getPageNum(), dataRuleDto.getPageSize());
        List<SysDataRule> list = ruleMapper.findList(map);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    /**
     * 新增菜单数据规则
     * @param sysDataRule 规则实体
     */
    @Override
    public void add(SysDataRule sysDataRule) {
        ruleMapper.insertSelective(sysDataRule);
    }

    /**
     * 修改菜单数据规则
     * @param sysDataRule 规则实体
     */
    @Override
    public void edit(SysDataRule sysDataRule) {
        if(null == sysDataRule.getId()){
            throw new BusinessException("更新数据规则失败，未找到相关资源。");
        }
        ruleMapper.updateByPrimaryKeySelective(sysDataRule);
    }

    /**
     * 根据数据规则ID删除数据规则信息
     * @param id 规则ID
     */
    @Override
    public void delete(Long id) {
        if(null == id){
            throw new BusinessException("更新数据规则失败，未找到相关资源。");
        }
        ruleMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据菜单ID删除菜单所有数据规则信息
     * @param permissionId 菜单ID
     * @return
     */
    @Override
    public int delRuleByPermissionId(Long permissionId){
        return ruleMapper.delRuleByPermissionId(permissionId);
    }

    /**
     * 根据菜单ID获取菜单所有数据规则信息
     * @param permissionId 菜单ID
     * @return
     */
    @Override
    public List<SysDataRule> selectByPermissionId(Long permissionId){
        return ruleMapper.selectByPermissionId(permissionId);
    }
    
    /**
     * 根据用户ID、菜单ID获取已经分配的数据规则
     * @param permissionId 菜单ID
     * @param userId 用户ID
     * @return
     */
    public List<SysDataRule> getPermDataRuleListByPermIdAndRoleId(Long permissionId,Long userId){
        List<SysDataRule> result = new ArrayList<SysDataRule>();
        // 增加非空校验
        if(userId != null && userId.intValue() > 0 && permissionId != null && permissionId.intValue() > 0){
            String userDataRuleIds = sysPermissionMapper.queryUserDataRuleIds(String.valueOf(userId),permissionId.intValue());
            if(StringUtils.isNotEmpty(userDataRuleIds)){
                String[] ids = userDataRuleIds.split(",");
                for(String id : ids){
                    SysDataRule sysDataRule = ruleMapper.selectByPrimaryKey(Long.parseLong(id));
                    result.add(sysDataRule);
                }
            }
        }
        return result;
    }
    /**
     * 根据数据规则ID获取到数据规则信息
     * @param id 数据规则主键ID
     * @return
     */
    public SysDataRule selectByPrimaryKey(Long id){
        return ruleMapper.selectByPrimaryKey(id);
    }
    /**
     * 根据数据规则状态 暂时无用。
     * @param id 规则ID
     * @param status 待更新状态
     */
    @Override
    public void updateStatus(Long id, Integer status) {
    }
}
